package com.newcapec.erp.ui;

import com.newcapec.erp.bean.InventoryEntity;
import com.newcapec.erp.main.DataInit;
import com.newcapec.erp.utils.XMLFReader;
import com.newcapec.erp.utils.XMLFWriter;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class UpdateInventoryUI extends JFrame {
    private JPanel contentPane;
    private JLabel label;
    private JLabel label_1;
    private JLabel label_2;
    private JLabel label_3;
    private JLabel label_4;
    private JLabel label_5;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private InventoryEntity inventoryEntity;
    private int index;
    private int currentPage;

    public UpdateInventoryUI(){}
    /**
     * Create the frame.
     */
    public UpdateInventoryUI(InventoryEntity inventoryEntity,int index,int currentPage) {
        this.inventoryEntity = inventoryEntity;
        this.index = index;
        this.currentPage = currentPage;
        setIconImage(Toolkit.getDefaultToolkit().getImage("images/logo.png"));
        setTitle("修改库存信息");
        setBounds(100, 100, 452, 568);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        label = new JLabel(DataInit.inventoryColumnNames.get(0));
        label.setBounds(20,34,100,37);
        textField = new JTextField();
        textField.setBounds(80, 34, 341, 37);
        textField.setText(inventoryEntity.getStockType());
        textField.setColumns(10);
        contentPane.add(label);
        contentPane.add(textField);


        label_1 = new JLabel(DataInit.inventoryColumnNames.get(1));
        label_1.setBounds(20,107,100,37);
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(80, 107, 341, 37);
        textField_1.setText(inventoryEntity.getCommodityCode());
        contentPane.add(label_1);
        contentPane.add(textField_1);

        label_2 = new JLabel(DataInit.inventoryColumnNames.get(2));
        label_2.setBounds(20,185,100,37);
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(80, 185, 341, 37);
        textField_2.setText(inventoryEntity.getProductName());
        contentPane.add(label_2);
        contentPane.add(textField_2);

        label_3 = new JLabel(DataInit.inventoryColumnNames.get(3));
        label_3.setBounds(20,254,100,37);
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(80, 254, 341, 37);
        textField_3.setText(inventoryEntity.getPCS());
        contentPane.add(label_3);
        contentPane.add(textField_3);

        label_4 = new JLabel(DataInit.inventoryColumnNames.get(4));
        label_4.setBounds(20,328,100,37);
        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(80, 328, 341, 37);
        textField_4.setText(inventoryEntity.getCustomerName());
        contentPane.add(label_4);
        contentPane.add(textField_4);

        label_5 = new JLabel(DataInit.inventoryColumnNames.get(5));
        label_5.setBounds(20,402,100,37);
        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(80, 402, 341, 37);
        textField_5.setText(inventoryEntity.getExpiringDate());
        contentPane.add(label_5);
        contentPane.add(textField_5);

        JButton button = new JButton("保存");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = textField.getText();
                String text1 = textField_1.getText();
                String text2 = textField_2.getText();
                String text3 = textField_3.getText();
                String text4 = textField_4.getText();
                String text5 = textField_5.getText();
                InventoryEntity newInventoryEntity = new InventoryEntity(text, text1, text2, text3, text4, text5);
                XMLFWriter.UpdateInventoryEntity(index,newInventoryEntity);
                JOptionPane.showMessageDialog(null, "恭喜你，修改成功.", "提示",JOptionPane.PLAIN_MESSAGE);
                Vector<Vector<String>> data = XMLFReader.getData(DataInit.TYPE_INVENTORY);
                MainUI.updateMainUI(DataInit.TYPE_INVENTORY,data,currentPage);
                dispose();
            }
        });
        button.setBounds(221, 481, 93, 23);
        contentPane.add(button);
        setLocationRelativeTo(null);
        setVisible(true);

    }
}
